﻿Delegate Function GetWindowState() As WindowState
Delegate Sub SetShowInTaskbar(ByVal value As Boolean)

Public Class Panel
    Dim ParentMainWindow As MainWindow
    Dim WithEvents StarterProperties As StarterProperties
    Dim IsNewStarter As Boolean
    Public StarterList As New List(Of Starter)
    Dim MousePos As Point
    Dim ClickedStarter As Starter
    WithEvents PanelProperties As PanelProperties
    Public IsLeftPanel As Boolean
    Dim IsNewPanel As Boolean

    Public Property ParentWindow As MainWindow
        Get
            MousePos = Mouse.GetPosition(Me)
            Return ParentMainWindow
        End Get

        Set(ByVal value As MainWindow)
            ParentMainWindow = value
        End Set
    End Property

    Private Sub DeletePanel(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MenuItem1.Click
        ParentMainWindow.DeleteItem(Me)
    End Sub

    Private Sub AddStarter(ByVal sender As System.Object, ByVal e As RoutedEventArgs) Handles MenuItem2.Click
        StarterProperties = New StarterProperties
        IsNewStarter = True
        StarterProperties.ShowDialog()
    End Sub

    Public Sub EditStarter(ByVal Starter As Starter)
        My.Settings.StarterPath = Starter.FileName
        StarterProperties = New StarterProperties
        IsNewStarter = False
        ClickedStarter = Starter
        StarterProperties.ShowDialog()
    End Sub

    Public Sub EditPanel()
        My.Settings.PanelSize = IIf(IsLeftPanel, Me.Width, Me.Height)
        My.Settings.PanelDock = DockPanel.GetDock(Me)
        IsNewPanel = False
        PanelProperties = New PanelProperties
        PanelProperties.ShowDialog()
    End Sub

    Private Sub OnSaveStarter(ByVal sender As System.Object, ByVal e As StarterProperties.StarterSaveEventArgs) Handles StarterProperties.StarterSaved
        If IsNewStarter Then
            If IsLeftPanel Then
                AddStarterFinish(e.FilePath, MousePos.Y)
            Else
                AddStarterFinish(e.FilePath, MousePos.X)
            End If
        Else
            Try
                ClickedStarter.FileName = e.FilePath
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub OnSavePanel(ByVal sender As System.Object, ByVal e As PanelProperties.PanelSaveEventArgs) Handles PanelProperties.PanelSaved
        If IsNewPanel Then
            ParentWindow.AddPanel(e.Height, e.Dock)
        Else
            ChangeDock(e.Dock, e.Height)
        End If
    End Sub

    Private Sub ChangeDock(ByVal Dock As Dock, ByVal NewSize As Integer)
        Dim OldLeftPanel As Boolean = IsLeftPanel
        IsLeftPanel = Dock = Controls.Dock.Left Or Dock = Controls.Dock.Right

        If IsLeftPanel Then
            Me.Width = NewSize
            Me.Height = Double.NaN
        Else
            Me.Height = NewSize
            Me.Width = Double.NaN
        End If

        For Each s As Starter In StarterList
            s.Width = NewSize
            s.Height = NewSize

            If Not OldLeftPanel = IsLeftPanel Then
                If IsLeftPanel Then
                    Canvas.SetTop(s, Canvas.GetLeft(s))
                    Canvas.SetLeft(s, 0)
                Else
                    Canvas.SetLeft(s, Canvas.GetTop(s))
                    Canvas.SetTop(s, 0)
                End If
            End If
        Next
        If IsLeftPanel Then
            Me.Background = New LinearGradientBrush(Color.FromArgb(255, 40, 40, 40), Colors.Black, 0)
        Else
            Me.Background = New LinearGradientBrush(Color.FromArgb(255, 40, 40, 40), Colors.Black, 90)
        End If
        DockPanel.SetDock(Me, Dock)
    End Sub

    Public Sub AddStarterFinish(ByVal FileName As String, ByVal x As Integer)
        Dim starter As New Starter
        starter.ParentPanel = Me
        starter.FileName = FileName
        If IsLeftPanel Then
            starter.Width = Me.Width
            starter.Height = Me.Width
        Else
            starter.Width = Me.Height
            starter.Height = Me.Height
        End If
        Canvas1.Children.Add(starter)
        If IsLeftPanel Then
            Canvas.SetLeft(starter, 0)
            Canvas.SetTop(starter, x)
        Else
            Canvas.SetLeft(starter, x)
            Canvas.SetTop(starter, 0)
        End If
        StarterList.Add(starter)
    End Sub

    Private Sub Me_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Me.MouseDown
        If e.MiddleButton = MouseButtonState.Pressed Then
            ParentMainWindow.WindowState = WindowState.Minimized
            ParentMainWindow.ShowInTaskbar = True
            Dim t As New System.Threading.Thread(AddressOf ClickWait)
            t.Start()
        End If
    End Sub

    Private Sub ClickWait()
        Dim stm As New SetShowInTaskbar(AddressOf SetShowInTaskbar)
        Dim gws As New GetWindowState(AddressOf GetWindowState)

        Do Until ParentMainWindow.Dispatcher.Invoke(gws) = WindowState.Normal

        Loop
        ParentMainWindow.Dispatcher.Invoke(stm, False)
    End Sub

    Private Sub SetShowInTaskbar(ByVal Value As WindowState)
        ParentMainWindow.ShowInTaskbar = False
    End Sub

    Private Function GetWindowState() As WindowState
        Return ParentMainWindow.WindowState
    End Function

    Private Sub ByeBye(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        ParentMainWindow.Close()
    End Sub

    Public Sub DeleteStarter(ByVal Starter As Starter)
        StarterList.Remove(Starter)
        Canvas1.Children.Remove(Starter)
    End Sub

    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MenuItem3.Click
        IsNewPanel = True
        PanelProperties = New PanelProperties
        PanelProperties.ShowDialog()
    End Sub

    Private Sub UserControl_MouseRightButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles MyBase.MouseRightButtonDown
        MousePos = e.GetPosition(Me)
    End Sub

    Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MenuItem4.Click
        EditPanel()
    End Sub

    Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MenuItem5.Click
        If MsgBox("Möchten Sie wirklich ALLES zurücksetzen?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "WinPanel - Frage") = MsgBoxResult.Yes Then
            My.Settings.Panels = "Panel 3 24" & vbCr & "EndPanel"
            My.Settings.Save()
            MsgBox("Zurückgesetzt." & vbCr & "Die Anwendung wird nun ohne speichern beendet.", MsgBoxStyle.Information, "WinPanel - Zurücksetzen")
            End
        End If
    End Sub

    Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MenuItem6.Click
        If MsgBox("Möchten Sie wirklich alle Starter entfernen?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "WinPanel - Frage") = MsgBoxResult.Yes Then
            Canvas1.Children.Clear()
            StarterList.Clear()
        End If
    End Sub

    Private Sub MenuItem7_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MenuItem7.Click
        If MsgBox("Möchten Sie wirklich nicht speichern?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "WinPanel - Frage") = MsgBoxResult.Yes Then
            End
        End If
    End Sub

    Private Sub Canvas1_Drop(ByVal sender As System.Object, ByVal e As System.Windows.DragEventArgs) Handles Me.Drop
        Dim FileNames() As String = e.Data.GetData(DataFormats.FileDrop)
        If IsLeftPanel Then
            AddStarterFinish(FileNames(0), Mouse.GetPosition(Canvas1).Y)
        Else
            AddStarterFinish(FileNames(0), Mouse.GetPosition(Canvas1).X)
        End If
    End Sub

    Private Sub Canvas1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.DragEventArgs) Handles Me.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effects = DragDropEffects.All
        End If
    End Sub
End Class
